package com.gezi.admin.module.app.file;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import com.gezi.admin.config.application.ApplicationUtil;
import com.gezi.admin.module.app.file.model.AppFileInfoModel;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @Author 格子代码
 * @createDate 2024/2/10 09:00
 * @contact 1424529270@qq.com
 */
@Slf4j
public class AppFileUtil {

    public static AppFileInfoModel getFileInfoByUrl(String url){
        String localPath = url.replaceAll(ApplicationUtil.fileWebUrl,ApplicationUtil.fileLocalPath);
        AppFileInfoModel appFileInfoModel = new AppFileInfoModel();
        appFileInfoModel.setUrl(url);
        appFileInfoModel.setLocalPath(localPath);
        appFileInfoModel.setName(FileUtil.getName(localPath));
        appFileInfoModel.setExtName(FileUtil.extName(localPath));
        appFileInfoModel.setMainName(FileUtil.mainName(localPath));
        appFileInfoModel.setType(FileUtil.getType(FileUtil.file(localPath)));
        appFileInfoModel.setFile(FileUtil.file(localPath));
        return appFileInfoModel;
    }

    public static String getUrlByFile(File file){
        String url = FileUtil.getAbsolutePath(file).replaceAll("\\\\","/").replace(ApplicationUtil.fileLocalPath,ApplicationUtil.fileWebUrl);
        return url;
    }

    public static void main(String[] args) {
        String fileLocalPath = "D:\\YanWe\\Desktop\\微信图片_20230606224422.png";
        log.info("name:{}",FileNameUtil.getName(fileLocalPath));
        log.info("Prefix:{}",FileNameUtil.getPrefix(fileLocalPath));
        log.info("Suffix:{}",FileNameUtil.getSuffix(fileLocalPath));
        log.info("mainName:{}",FileNameUtil.mainName(fileLocalPath));
        log.info("extName:{}",FileNameUtil.extName(fileLocalPath));
    }
}
